﻿using System;
using System.Collections.Generic;
using System.Linq;
using ExtAspNet;
using Research.BLL;
using Research.Model;

namespace Research.Web
{
    public partial class RprjDetails : System.Web.UI.Page
    {
        readonly BasicBll _bllBasic = new BasicBll();

        readonly RprjMainBll _bllRprjMain = new RprjMainBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindchkSubjectList();
                BindchkDepList();
                BindddlPlatform();
                BindddlRprjRole();
                BindddlFund();
                btnChargePerDel.OnClientClick = gridChargePer.GetNoSelectionAlertInTopReference("没有选中项!");
                btnPrjDel.OnClientClick = gridPrj.GetNoSelectionAlertInTopReference("没有选中项!");
                btnFundComeInDel.OnClientClick = gridFundComeIn.GetNoSelectionAlertInTopReference("没有选中项!");


                if (Request["Type"]=="Update")
                {
                    var mRprjMain = _bllBasic.GetRprjMainById(Convert.ToInt32(Request["rPrjID"]));
                    BindBasicInfo(mRprjMain);

                    BindGridChargePer(mRprjMain.ChargePers);
                    BindGridPrj(mRprjMain.Prjs);
                    BindGridFundComeIn(mRprjMain.Funds);
                    BindSelectChkDepList(mRprjMain.Deps);
                    BindSelectChkSubjectList(mRprjMain.Subjects);

                    ViewState["MRprjChargePers"] = mRprjMain.ChargePers;
                    ViewState["MRprjFunds"] = mRprjMain.Funds;
                    ViewState["MRprjPrjs"] = mRprjMain.Prjs;
                }



            }
        }

        protected void twinTrgBoxSearchPer_Trigger1Click(object sender, EventArgs e)
        {
            twinTrgBoxSearchPer.Text = String.Empty;
            twinTrgBoxSearchPer.ShowTrigger1 = false;
            gridSearchPer.DataSource = null;
            gridSearchPer.Hidden = true;
        }

        protected void twinTrgBoxSearchPer_Trigger2Click(object sender, EventArgs e)
        {
            twinTrgBoxSearchPer.ShowTrigger1 = true;
            gridSearchPer.Hidden = false;
            gridSearchPer.DataSource = _bllBasic.GetChargePerList(twinTrgBoxSearchPer.Text);
            gridSearchPer.DataBind();
        }

        protected void gridSearchPer_RowClick(object sender, ExtAspNet.GridRowClickEventArgs e)
        {
            var chargePer = new MRprjChargePer { ChargePerID = gridSearchPer.DataKeys[e.RowIndex][0].ToString(), ChargePerName = gridSearchPer.DataKeys[e.RowIndex][1].ToString() };

            List<MRprjChargePer> chargePers;
            if (ViewState["MRprjChargePers"] != null)
                chargePers = (List<MRprjChargePer>)ViewState["MRprjChargePers"];
            else
                chargePers = new List<MRprjChargePer>();

            var query = from c in chargePers where c.ChargePerID == chargePer.ChargePerID select c; 

            if (!query.Any())
            {
                chargePers.Add(chargePer);
                ViewState["MRprjChargePers"] = chargePers;
                BindGridChargePer(chargePers);
                //gridChargePer.DataSource = chargePers;
                //gridChargePer.DataBind();
            }

            gridSearchPer.DataSource = null;
            gridSearchPer.Hidden = true;
        }
        
        protected void btnChargePerDel_Click(object sender, EventArgs e)
        {
            if (gridChargePer.SelectedRowIndexArray.Length>0)
            {
                gridChargePer.Rows.RemoveAt(gridChargePer.SelectedRowIndexArray[0]);
            }
        }
        
        protected void twinTrgBoxSearchPrj_Trigger1Click(object sender, EventArgs e)
        {
            twinTrgBoxSearchPrj.Text = String.Empty;
            twinTrgBoxSearchPrj.ShowTrigger1 = false;
        }

        protected void twinTrgBoxSearchPrj_Trigger2Click(object sender, EventArgs e)
        {
            twinTrgBoxSearchPrj.ShowTrigger1 = true;
            var prjs = _bllBasic.GetPrjList(twinTrgBoxSearchPrj.Text);

            if (prjs.Count>0)
            {
                var mPrj = new MRprjPrj { PrjID = prjs[0].PrjID, PrjName = prjs[0].PrjName, PrjNum = prjs[0].PrjNum };
                List<MRprjPrj> mPrjs;
                if (ViewState["MRprjPrjs"] != null)
                    mPrjs = (List<MRprjPrj>)ViewState["MRprjPrjs"];
                else
                    mPrjs = new List<MRprjPrj>();

                var query = from c in mPrjs where c.PrjID == mPrj.PrjID select c;

                if (!query.Any())
                {
                    mPrjs.Add(mPrj);
                    ViewState["MRprjPrjs"] = mPrjs;
                    BindGridPrj(mPrjs);
                    //gridPrj.DataSource = mPrjs;
                    //gridPrj.DataBind();
                }
            }
            else
            {
                ExtAspNet.Alert.Show("没有此工程!");
            }
        }

        protected void btnPrjDel_Click(object sender, EventArgs e)
        {
            if (gridPrj.SelectedRowIndexArray.Length > 0)
            {
                gridPrj.Rows.RemoveAt(gridPrj.SelectedRowIndexArray[0]);
            }
        }

        protected void btnFundComeInAdd_Click(object sender, EventArgs e)
        {
            var mFund = new MRprjFund
                            {
                                FundID = Convert.ToInt16(ddlFund.SelectedValue),
                                FundName = ddlFund.SelectedText,
                                Amount = Convert.ToDouble(nbFundAmount.Text),
                                AddDate = dpAddDate.SelectedDate.ToString(),
                                Memo = txtMemo.Text
                            };
            List<MRprjFund> mFunds;
            if (ViewState["MRprjFunds"] != null)
                mFunds = (List<MRprjFund>)ViewState["MRprjFunds"];
            else
                mFunds = new List<MRprjFund>();

            var query = from c in mFunds where c.AddDate == mFund.AddDate && c.FundID == mFund.FundID select c;

            if (!query.Any())
            {
                mFunds.Add(mFund);
                ViewState["MRprjFunds"] = mFunds;
                BindGridFundComeIn(mFunds);
                //gridFundComeIn.DataSource = mFunds;
                //gridFundComeIn.DataBind();
            }
        }

        protected void btnFundComeInDel_Click(object sender, EventArgs e)
        {
            if (gridFundComeIn.SelectedRowIndexArray.Length > 0)
            {
                gridFundComeIn.Rows.RemoveAt(gridFundComeIn.SelectedRowIndexArray[0]);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request["Type"] == "Add")
                AddRprj();
            else if (Request["Type"] == "Update")
                UpdateRprj();
        }

        public void AddRprj()
        {
            var mRPrjMain = new MRprjMain
                                {
                                    RprjName = txtRprjName.Text,
                                    RprjNum = txtRprjNum.Text,
                                    RprjPlatformID = Convert.ToInt16(ddlPlatform.SelectedValue),
                                    RprjRoleID = Convert.ToInt16(ddlRprjRole.SelectedValue)
                                };
            mRPrjMain.ChargePers = (List<MRprjChargePer>) ViewState["MRprjChargePers"];
            mRPrjMain.Funds = (List<MRprjFund>) ViewState["MRprjFunds"];
            mRPrjMain.Prjs = (List<MRprjPrj>) ViewState["MRprjPrjs"];

            var deps =
                chkDepList.SelectedValueArray.Select(depValue => new MRprjDep {DepID = Convert.ToInt16(depValue)}).ToList();
            var subjects =
                chkSubjectList.SelectedValueArray.Select(
                    subjectValue => new MRprjSubject {RprjSubjectID = Convert.ToInt16(subjectValue)}).ToList();

            mRPrjMain.Deps = deps;
            mRPrjMain.Subjects = subjects;

            if (_bllRprjMain.InsertRprj(mRPrjMain))
                ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHideRefreshReference());
            else
                ExtAspNet.Alert.Show("新增未成功！");
        }

        public void UpdateRprj()
        {
            var mRPrjMain = new MRprjMain
            {
                RprjID = Convert.ToInt32(Request["RprjID"]),
                RprjName = txtRprjName.Text,
                RprjNum = txtRprjNum.Text,
                RprjPlatformID = Convert.ToInt16(ddlPlatform.SelectedValue),
                RprjRoleID = Convert.ToInt16(ddlRprjRole.SelectedValue)
            };
            mRPrjMain.ChargePers = (List<MRprjChargePer>)ViewState["MRprjChargePers"];
            mRPrjMain.Funds = (List<MRprjFund>)ViewState["MRprjFunds"];
            mRPrjMain.Prjs = (List<MRprjPrj>)ViewState["MRprjPrjs"];

            var deps =
                chkDepList.SelectedValueArray.Select(depValue => new MRprjDep { DepID = Convert.ToInt16(depValue) }).ToList();
            var subjects =
                chkSubjectList.SelectedValueArray.Select(
                    subjectValue => new MRprjSubject { RprjSubjectID = Convert.ToInt16(subjectValue) }).ToList();

            mRPrjMain.Deps = deps;
            mRPrjMain.Subjects = subjects;

            if (_bllRprjMain.UpdateRprj(mRPrjMain))
                ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHideRefreshReference());
            else
                ExtAspNet.Alert.Show("修改未成功！");
        }


        private void BindchkSubjectList()
        {
            using (var ds = _bllBasic.GetSubjectList())
            {
                chkSubjectList.DataTextField = "SubjectName";
                chkSubjectList.DataValueField = "SubjectID";
                chkSubjectList.DataSource = ds;
                chkSubjectList.DataBind();
            }
        }

        private void BindchkDepList()
        {
            using (var ds = _bllBasic.GetDepList())
            {
                chkDepList.DataTextField = "DepartName";
                chkDepList.DataValueField = "DepID";
                chkDepList.DataSource = ds;
                chkDepList.DataBind();
            }
        }

        private void BindddlPlatform()
        {
            using (var ds = _bllBasic.GetPlatformList())
            {
                ddlPlatform.DataTextField = "PlatformName";
                ddlPlatform.DataValueField = "PlatformID";
                ddlPlatform.DataSource = ds;
                ddlPlatform.DataBind();
            }
        }

        private void BindddlRprjRole()
        {
            using (var ds = _bllBasic.GetRprjRoleList())
            {
                ddlRprjRole.DataTextField = "RoleName";
                ddlRprjRole.DataValueField = "RprjRoleID";
                ddlRprjRole.DataSource = ds;
                ddlRprjRole.DataBind();
            }
        }

        private void BindddlFund()
        {
            using (var ds = _bllBasic.GetFundList())
            {
                ddlFund.DataTextField = "FundName";
                ddlFund.DataValueField = "FundID";
                ddlFund.DataSource = ds;
                ddlFund.DataBind();
            }
        }
        
        private void BindGridPrj(List<MRprjPrj> mPrjs)
        {
            gridPrj.DataSource = mPrjs;
            gridPrj.DataBind();
        }

        private void BindGridChargePer(List<MRprjChargePer> chargePers)
        {
            gridChargePer.DataSource = chargePers;
            gridChargePer.DataBind();
        }

        private void BindGridFundComeIn(List<MRprjFund> mFunds)
        {
            gridFundComeIn.DataSource = mFunds;
            gridFundComeIn.DataBind();
        }

        private void BindBasicInfo(MRprjMain mRprjMain)
        {
            txtRprjNum.Text = mRprjMain.RprjNum;
            txtRprjName.Text = mRprjMain.RprjName;
            ddlPlatform.SelectedValue = mRprjMain.RprjPlatformID.ToString();
            ddlRprjRole.SelectedValue = mRprjMain.RprjRoleID.ToString();
        }

        private void BindSelectChkDepList(List<MRprjDep> mDeps)
        {
            var selects = new string[mDeps.Count];

            for (var i=0;i<mDeps.Count;i++)
            {
                selects[i] = mDeps[i].DepID.ToString();
            }

            chkDepList.SelectedValueArray = selects;
        }

        private void BindSelectChkSubjectList(List<MRprjSubject> mSubjects)
        {
            var selects = new string[mSubjects.Count];

            for (var i = 0; i < mSubjects.Count; i++)
            {
                selects[i] = mSubjects[i].RprjSubjectID.ToString();
            }

            chkSubjectList.SelectedValueArray = selects;
        }

    }
}